<?php

declare(strict_types=1);

namespace app\admin\service\auth;

use Dengje\Pxy\BaseService;
use think\facade\Db;

class OperLogService extends BaseService
{
    protected $model = null;


    public function __construct()
    {

        $this->model =  new \app\common\model\OperLogModel();
    }

    public function list()
    {
        $where = [];
        $account = request()->get('account');
        if ($account) $where[] = ['account', '=', $account];
        $router = request()->get('router');
        if ($router) $where[] = ['router', '=', $router];
        if (request()->get('page_size/d')) {
            $list = $this->model->where($where)->order(['id' => 'desc'])->paginate(request()->get('page_size/d', 10));
        } else {
            $list = $this->model->where($where)->order(['id' => 'desc'])->select();
        }
        return $list;
    }

    public function create($data)
    {
        return $this->model->create($data);
    }

    public function update($id, $data)
    {
        $data = $this->preExcludeFields($data);
        return $this->model->where('id', $id)->update($data);
    }

    public function detail($id)
    {
        $detail = $this->model->where('id', $id)->find();
        return $detail;
    }

    public function delete($id)
    {
      
        Db::startTrans();
        try{
            
            $role =  $this->model->where('id', $id)->delete();
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            throw new \Exception($e->getMessage());
        }
        
        return  $role;
    }
}
